﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
//using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;
//using System.ComponentModel.DataAnnotations;
using CodeCamp.Model;
using RGoodSW.Logger.Models;

namespace CodeCamp.EFDAL
{
#if true
    public class CodeCampDbContext : DbContext
    {
        public CodeCampDbContext()
            : base()
        {
        }

        public CodeCampDbContext(string connectionString)
            : base(connectionString)
        {
        }

        public DbSet<BannedTag> BannedTags { get; set; }
        public DbSet<Building> Buildings { get; set; }
        public DbSet<CommunitySponsor> CommunitySponsors { get; set; }
        public DbSet<EmailCampaign> EmailCampaigns { get; set; }
        public DbSet<EmailItem> EmailItems { get; set; }
        public DbSet<Event> Events { get; set; }
        public DbSet<IndividualContributor> IndividualContributors { get; set; }
        public DbSet<LogEntry> LogEntries { get; set; }
        public DbSet<Presenter> Presenters { get; set; }
        public DbSet<PresenterBiography> PresenterBiographies { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<Role4Event> Roles4Events { get; set; }
        public DbSet<Room> Rooms { get; set; }
        public DbSet<ScheduledSession> ScheduledSessions { get; set; }
        public DbSet<Session> Sessions { get; set; }
        public DbSet<SessionPresenter> SessionPresenters { get; set; }
        public DbSet<SpeakerRateEvent> SpeakerRateEvents { get; set; }
        public DbSet<SpeakerRateSpeaker> SpeakerRateSpeakers { get; set; }
        public DbSet<SessionTag> SessionTags { get; set; }
        public DbSet<SiteUser> SiteUsers { get; set; }
        public DbSet<Sponsor> Sponsors { get; set; }
        //public DbSet<ShirtSize> ShirtSizes { get; set; }
        public DbSet<Tag> Tags { get; set; }
        public DbSet<Task> Tasks { get; set; }
        public DbSet<Timeslot> Timeslots { get; set; }
        public DbSet<UserInEventRole> UsersInEventRoles { get; set; }
        public DbSet<UserInRole> UsersInRoles { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<LogEntry>()
                .HasKey(e => e.LogEntryID)
                .Property(e => e.Application)
                .IsUnicode(false);
            modelBuilder.Entity<LogEntry>()
                .ToTable("aspnetRgs_log4net");
            modelBuilder.Entity<LogEntry>()
                .Property(e => e.Logger)
                .IsUnicode(false);
            modelBuilder.Entity<LogEntry>()
                .Property(e => e.UserName)
                .IsUnicode(false);
            modelBuilder.Entity<LogEntry>()
                .Property(e => e.RemoteIP)
                .IsUnicode(false);
            modelBuilder.Entity<LogEntry>()
                .Property(e => e.MachineName)
                .IsUnicode(false);
            modelBuilder.Entity<LogEntry>()
                .Property(e => e.SessionId)
                .IsUnicode(false);
            modelBuilder.Entity<LogEntry>()
                .Property(e => e.URL)
                .IsUnicode(false);
            modelBuilder.Entity<LogEntry>()
                .Property(e => e.Message)
                .IsUnicode(false);
            modelBuilder.Entity<LogEntry>()
                .Property(e => e.Exception)
                .IsUnicode(false);

            modelBuilder.Entity<BannedTag>()
                .HasKey(e => e.Id)
                .ToTable("cc_BannedTags");

            modelBuilder.Entity<Building>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Event)
                .WithMany(e => e.Buildings)
                .HasForeignKey(e => e.EventId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<Building>()
                .ToTable("cc_Buildings");

            modelBuilder.Entity<CommunitySponsor>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Event)
                .WithMany(e => e.CommunitySponsors)
                .HasForeignKey(e => e.EventId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<CommunitySponsor>()
                .ToTable("cc_CommunitySponsors");

            modelBuilder.Entity<EmailCampaign>()
                .HasKey(e => e.Id)
                .ToTable("cc_EmailCampaigns");
            modelBuilder.Entity<EmailCampaign>()
                .HasRequired(e => e.Event)
                .WithMany(e => e.EmailCampaigns)
                .HasForeignKey(e => e.EventId)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<EmailItem>()
                .HasKey(e => e.Id)
                .ToTable("cc_EmailItems");
            modelBuilder.Entity<EmailItem>()
                .HasRequired(e => e.Event)
                .WithMany(e => e.EmailItems)
                .HasForeignKey(e => e.EventId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<EmailItem>()
                .HasOptional(e => e.EmailCampaign)
                .WithMany(e => e.EmailItems)
                .HasForeignKey(e => e.EmailCampaignId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<EmailItem>()
                .HasOptional(e => e.SiteUser)
                .WithMany(e => e.EmailItems)
                .HasForeignKey(e => e.SiteUserId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<EmailItem>()
                .HasOptional(e => e.Presenter)
                .WithMany(e => e.EmailItems)
                .HasForeignKey(e => e.PresenterId)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Event>()
                .HasKey(e => e.Id)
                .ToTable("cc_Events");

            modelBuilder.Entity<IndividualContributor>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Event)
                .WithMany(e => e.IndividualContributors)
                .HasForeignKey(e => e.EventId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<IndividualContributor>()
                .ToTable("cc_IndividualContributors");

            modelBuilder.Entity<Presenter>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Event)
                .WithMany(e => e.Presenters)
                .HasForeignKey(e => e.EventId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<Presenter>()
                .HasOptional(e => e.SiteUser)
                .WithMany(e => e.Presenters)
                .HasForeignKey(e => e.SiteUserId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<Presenter>()
                .ToTable("cc_Presenters");

            modelBuilder.Entity<PresenterBiography>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Presenter)
                .WithMany(e => e.PresenterBiographies)
                .HasForeignKey(e => e.PresenterId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<PresenterBiography>()
                .ToTable("cc_PresenterBiographies");

            modelBuilder.Entity<Role>()
                .HasKey(e => e.Id)
                .ToTable("cc_Roles");

            modelBuilder.Entity<Role4Event>()
                .HasKey(e => e.Id)
                .ToTable("cc_Roles4Events");

            modelBuilder.Entity<Room>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Building)
                .WithMany(e => e.Rooms)
                .HasForeignKey(e => e.BuildingId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<Room>()
                .ToTable("cc_Rooms");

            modelBuilder.Entity<ScheduledSession>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Room)
                .WithMany(e => e.ScheduledSessions)
                .HasForeignKey(e => e.RoomId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<ScheduledSession>()
                .HasRequired(e => e.Session)
                .WithMany(e => e.ScheduledSessions)
                .HasForeignKey(e => e.SessionId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<ScheduledSession>()
                .HasRequired(e => e.Timeslot)
                .WithMany(e => e.ScheduledSessions)
                .HasForeignKey(e => e.TimeslotId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<ScheduledSession>()
                .ToTable("cc_ScheduledSessions");

            modelBuilder.Entity<Session>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Event)
                .WithMany(e => e.Sessions)
                .HasForeignKey(e => e.EventId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<Session>()
                .ToTable("cc_Sessions");

            modelBuilder.Entity<SessionPresenter>()
                .HasKey(d => new { d.PresenterId, d.SessionId });
            modelBuilder.Entity<SessionPresenter>()
                .HasRequired(e => e.Presenter)
                .WithMany(e => e.SessionPresenters)
                .HasForeignKey(e => e.PresenterId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<SessionPresenter>()
                .HasRequired(e => e.Session)
                .WithMany(e => e.SessionPresenters)
                .HasForeignKey(e => e.SessionId)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<SessionPresenter>()
                .ToTable("cc_SessionPresenters");

            modelBuilder.Entity<SessionTag>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Session)
                .WithMany(e => e.SessionTags)
                .HasForeignKey(e => e.SessionId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<SessionTag>()
                .HasRequired(e => e.Tag)
                .WithMany(e => e.SessionTags)
                .HasForeignKey(e => e.TagId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<SessionTag>()
                .ToTable("cc_SessionTags");

            modelBuilder.Entity<SiteUser>()
                .HasKey(e => e.Id)
                .Property(e => e.Version)
                .IsConcurrencyToken();
            modelBuilder.Entity<SiteUser>()
                .ToTable("cc_SiteUsers");

            modelBuilder.Entity<SpeakerRateEvent>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Event)
                .WithMany(e => e.SpeakerRateEvents)
                .HasForeignKey(e => e.EventId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<SpeakerRateEvent>()
                .ToTable("cc_SpeakerRateEvent");

            modelBuilder.Entity<SpeakerRateSpeaker>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Presenter)
                .WithMany(e => e.SpeakerRateSpeakers)
                .HasForeignKey(e => e.PresenterId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<SpeakerRateSpeaker>()
                .ToTable("cc_SpeakerRateSpeakers");

            modelBuilder.Entity<SpeakerRateTalk>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Session);
            modelBuilder.Entity<SpeakerRateTalk>()
                .ToTable("cc_SpeakerRateTalks");

            modelBuilder.Entity<Sponsor>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Event)
                .WithMany(e => e.Sponsors)
                .HasForeignKey(e => e.EventId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<Sponsor>()
                .ToTable("cc_Sponsors");

            modelBuilder.Entity<SponsorImage>()
                .HasKey(e => e.Id)
                .ToTable("cc_SponsorImages");

            modelBuilder.Entity<Tag>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Event)
                .WithMany(e => e.Tags)
                .HasForeignKey(e => e.EventId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<Tag>()
                .ToTable("cc_Tags");

            modelBuilder.Entity<Task>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Event)
                .WithMany(e => e.Tasks)
                .HasForeignKey(e => e.EventId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<Task>()
                .ToTable("cc_Tasks");

            modelBuilder.Entity<Tasks_Timeslot>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Task)
                .WithMany(e => e.Tasks_Timeslots)
                .HasForeignKey(e => e.TaskId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<Tasks_Timeslot>()
                .HasRequired(e => e.TaskTimeslot)
                .WithMany(e => e.Tasks_Timeslots)
                .HasForeignKey(e => e.TaskTimeslotId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<Tasks_Timeslot>()
                .ToTable("cc_Tasks_TimeSlots");

            modelBuilder.Entity<TaskTimeslot>()
                .HasKey(e => e.Id)
                .ToTable("cc_TaskTimeSlots");

            modelBuilder.Entity<Timeslot>()
                .HasKey(e => e.Id)
                .HasRequired(e => e.Event)
                .WithMany(e => e.Timeslots)
                .HasForeignKey(e => e.EventId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<Timeslot>()
                .ToTable("cc_TimeSlots");

            modelBuilder.Entity<UserInEventRole>()
                .HasKey(e => new { e.Role4EventId, e.SiteUserId })
                .HasRequired(e => e.Event);
            modelBuilder.Entity<UserInEventRole>()
                .HasRequired(e => e.SiteUser);
            modelBuilder.Entity<UserInEventRole>()
                .ToTable("cc_UsersInEventRoles");

            modelBuilder.Entity<UserInRole>()
                .HasKey(e => new { e.RoleId, e.SiteUserId })
                .HasRequired(e => e.Role);
            modelBuilder.Entity<UserInRole>()
                .HasRequired(e => e.SiteUser);
            modelBuilder.Entity<UserInRole>()
                .ToTable("cc_UsersInRoles");

#if false
            modelBuilder.Entity<BannedTag>()
                .ToTable("");
#endif    
            
            

            base.OnModelCreating(modelBuilder);

            //modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

            //modelBuilder.Entity<Session>()
            //    .HasRequired(s => s.Event)
            //    .WithMany(e => e.Sessions)
            //    .WillCascadeOnDelete(false);

            //modelBuilder.Entity<Session>()
            //    .HasMany(s => s.Presenters)
            //    .WithRequired(sp => sp.Presenter)
            //    .WillCascadeOnDelete(false);
        }
    }
#endif
}
